package com.feeyin.radar.crm.dao.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.feeyin.radar.crm.dao.ContractInvoiceDAO;
import com.feeyin.radar.crm.model.Invoice;
import com.feeyin.radar.crm.model.InvoiceCase;

public class ContractInvoiceDAOImpl extends SqlMapClientDaoSupport
  implements ContractInvoiceDAO
{
  public List getInvoices(Integer contractId)
  {
    if (contractId == null) return null;
    return getSqlMapClientTemplate().queryForList("getInvoices", contractId);
  }

  public Date getMinInvoiceDate(Integer contractId) {
    if (contractId == null) return null;
    return (Date)getSqlMapClientTemplate().queryForObject("getMinInvoiceDate", contractId);
  }

  public BigDecimal getTotalInvoiceCost(Integer contractId) {
    if (contractId == null) return null;
    return (BigDecimal)getSqlMapClientTemplate().queryForObject("getTotalInvoiceCost", contractId);
  }

  public boolean insertInvoice(Invoice invoice) {
    if (invoice == null) return false;
    Integer result = (Integer)getSqlMapClientTemplate().insert("insertInvoice", invoice);
    
    for(InvoiceCase ic : invoice.getInvoiceCaseList())
    {
    	ic.setInvoiceId(result);
    	getSqlMapClientTemplate().insert("insertInvoiceCase", ic);
    }
    return result != null;
  }
  
  public Invoice getInvoiceById(Integer id)
  {
    if (id == null) return null;
    return (Invoice)getSqlMapClientTemplate().queryForObject("getInvoiceById", id);
  }  
}